package com.uplift.mgt.building.service.impl;

import com.github.pagehelper.PageHelper;
import com.uplift.common.base.ServiceReslut;
import com.uplift.mgt.building.dao.BuildingMapper;
import com.uplift.mgt.building.model.BuildingModel;
import com.uplift.mgt.building.service.IBuildingService;
import com.uplift.sec.SecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class BuildingServiceImpl implements IBuildingService {

	@Autowired
	private BuildingMapper buildingMapper;

	@Override
	public List<Map<String, Object>> queryBuilding(BuildingModel buildingModel) {
		PageHelper.startPage(buildingModel.getPage(), buildingModel.getLimit());
		return this.buildingMapper.selectBuilding(buildingModel);
	}

	@Override
	public List<Map<String, Object>> queryAllBuilding(BuildingModel buildingModel) {
		return this.buildingMapper.selectBuilding(buildingModel);
	}

	@Override
	public ServiceReslut addBuilding(BuildingModel buildingModel) {
		BuildingModel queryBuilding = new BuildingModel();
		queryBuilding.setCutomerName(buildingModel.getCutomerName());
		queryBuilding.setName(buildingModel.getName());
		List<Map<String, Object>> maps = this.buildingMapper.selectBuildingEqual(queryBuilding);
		if(maps!=null && maps.size()>0) {
			return ServiceReslut.newServiceReslut(0, "此用户下已存在相同的楼盘信息");
		}

		Map<String, Object> sessionUser = SecurityContext.getSessionUser();
		if(sessionUser == null) {
			return ServiceReslut.newServiceReslut(0, "获取登录用户信息失败");
		}

		buildingModel.setCreateUserId((String) sessionUser.get("id"));
		buildingModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		buildingModel.setState("1");
		buildingModel.setCreateTime(new Date());
		return ServiceReslut.newServiceReslut(buildingMapper.insertBuilding(buildingModel), "");
	}

	@Override
	public ServiceReslut updateBuilding(BuildingModel buildingModel) {
		buildingModel.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(buildingMapper.updateBuilding(buildingModel), "");
	}

	@Override
	public ServiceReslut deleteBuilding(BuildingModel buildingModel) {
		buildingModel.setState("0");		//删除状态
		buildingModel.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(buildingMapper.updateBuilding(buildingModel), "");
	}

}
